package com.zhen;

import com.zhen.domain.User;
import com.zhen.factory.FKSqlSessionFactory;
import com.zhen.mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;

public class TestOneLevelCache {

    public static void main(String[] args) {
        TestOneLevelCache t = new TestOneLevelCache();
//        t.testCache1();
//        t.testCache2();
        t.testCache3();
    }

    public void testCache1(){
        SqlSession session = FKSqlSessionFactory.getSqlSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        User user = userMapper.selectUserById(1);
        System.out.println(user);

        User user2 = userMapper.selectUserById(1);
        System.out.println(user);
        session.close();
    }

    public void testCache2(){
        SqlSession session = FKSqlSessionFactory.getSqlSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        User user = userMapper.selectUserById(1);
        System.out.println(user);

        userMapper.deleteUserById(5);
        session.commit();

        User user2 = userMapper.selectUserById(1);
        System.out.println(user2);
        session.close();
    }

    public void testCache3(){
        SqlSession session = FKSqlSessionFactory.getSqlSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        User user = userMapper.selectUserById(1);
        System.out.println(user);
        session.close();

        session = FKSqlSessionFactory.getSqlSession();
        userMapper = session.getMapper(UserMapper.class);

        User user2 = userMapper.selectUserById(1);
        System.out.println(user2);
        session.close();
    }
}
